/*  ----------------------------------------------------------------------------
 *  Arello Mobile
 *  Fast 3d Engine
 *  ----------------------------------------------------------------------------
 * Except where otherwise noted, this work is licensed under
 * http://creativecommons.org/licenses/by/3.0
 */

#ifndef __BOX_H__
#define __BOX_H__

#include "Vector3.h"
#include "Matrix4.h"

namespace liba
{
namespace math
{

class Box
{
	Vector3<float> nx;
	Vector3<float> ny;
	Vector3<float> nz;

	Vector3<float> center;
	Vector3<float> extents;

public:
	explicit Box( const Vector3<float> & extents, const Vector3<float> & center = Vector3<float>( 0, 0, 0 ) )
	:	nx( 1, 0, 0 ),
		ny( 0, 1, 0 ),
		nz( 0, 0, 1 ),
		center( center ),
		extents( extents )
	{}
	Box( const Vector3<float> & nx, const Vector3<float> & ny, const Vector3<float> & nz, const Vector3<float> & extents, const Vector3<float> & center )
	:	nx( nx ),
		ny( ny ),
		nz( ny ),
		center( center ),
		extents( extents )
	{}

	const Vector3<float> & get_x_normal()const
	{
		return nx;
	}
	const Vector3<float> & get_y_normal()const
	{
		return ny;
	}
	const Vector3<float> & get_z_normal()const
	{
		return nz;
	}
	const Vector3<float> & get_extents()const
	{
		return extents;
	}
	const Vector3<float> & get_center()const
	{
		return center;
	}

	void transform( const Matrix4<float> & transform )
	{
		Matrix4<float> hre;//workaround internal compiler error
		center = transform * center;

		Matrix4<float> mat = Matrix4<float>::translate( -transform.shift() ) * transform;

		nx = mat * nx;
		extents.x *= nx.length();
		nx.normalize();

		ny = mat * ny;
		extents.y *= ny.length();
		ny.normalize();

		nz = mat * nz;
		extents.y *= nz.length();
		nz.normalize();
	}
};

} // namespace math
} // namespace liba
using namespace liba;

#endif //__BOX_H__